<template>
  <div class="coach-info">
    <el-form ref="dataForm" :model="activity" :rules="rules" label-width="100px" style="padding-right: 40px;">
      <el-form-item label="活动图" prop="cover">
        <el-upload class="avatar-uploader" action="/dev-api/admin/common/upload" :show-file-list="false"
                   :on-success="handleAvatarSuccess" :before-upload="beforeAvatarUpload">
          <img v-if="activity.cover" :src="activity.cover" class="avatar">
          <i v-else class="el-icon-plus avatar-uploader-icon" />
        </el-upload>
      </el-form-item>
      <el-form-item label="活动页地址" prop="url" style="width:400px">
        <el-input v-model="activity.url" />
      </el-form-item>
      <el-form-item label="类别" prop="type" style="width:400px">
        <el-select v-model="activity.types" placeholder="请选择">
          <el-option :key="0" label="轮播图" :value="0" />
          <el-option :key="1" label="推荐图" :value="1" />
        </el-select>
      </el-form-item>
    </el-form>

    <span slot="footer" class="dialog-footer">
            <el-button size="small" icon="el-icon-refresh-right" @click="cancel">取 消</el-button>
            <el-button type="primary" icon="el-icon-check" size="small" @click="saveOrUpdate('dataForm')">确
                定</el-button>
        </span>
  </div>
</template>

<script>
import activityApi from '@/api/content/activity'

const defaultForm = {
  id: '',
  cover: '',
  url: '',
  status: '',
  types: '',
}

export default {
  data() {
    return {
      activity: defaultForm,
      dialogVisible: false,
      dialogImageUrl: '',

      rules: {
        types: [
          { required: true, message: '请选择类别', trigger: 'change' }
        ],
      }
    }
  },
  mounted() {
    if (!this.$route.query.id) {
      this.activity = defaultForm
    } else {
      this.fetchData(this.$route.query.id)
    }
  },
  methods: {
    fetchData(id) {
      activityApi.getById(id).then(
        response => {
          this.activity = response.data
        }
      )
    },
    // 添加或更新
    saveOrUpdate(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          if (!this.activity.id) {
            this.save(formName)
          } else {
            this.update(formName)
          }
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    // 添加
    save() {
      activityApi.save(this.activity).then(response => {
        this.$message.success(response.message || '操作成功')
        this.$router.replace('/content/activity')
      })
    },
    // 更新
    update(formName) {
      activityApi.updateById(this.activity).then(response => {
        this.$message.success(response.message || '操作成功')
        this.$router.replace('/content/activity')
      })
    },
    // 取消
    cancel() {
      this.$router.go(-1)
    },

    // 上传图片成功
    handleAvatarSuccess(res, file) {
      console.log(res)
      this.activity.cover = res.data
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === 'image/jpeg'
      const isLt2M = file.size / 1024 / 1024 < 2

      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG 格式!')
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!')
      }
      return isJPG && isLt2M
    },
    handlePictureCardPreview(file) {
      this.dialogImageUrl = file.url
      this.dialogVisible = true
    }
  }
}
</script>

<style lang="scss" scoped>
.coach-info {
  margin: 30px;
  margin-top: 40px;

  .dialog-footer {
    margin-left: 40px;
    margin-bottom: 20px;
  }
}
</style>

<style>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}

.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 128px;
  height: 128px;
  line-height: 128px;
  text-align: center;
}

.avatar {
  width: 128px;
  height: 128px;
  display: block;
}
</style>
